/**
 * 
 */
package com.huawei.struts.service.imp;

import java.util.List;

import com.huawei.struts.dao.CommonDAO;
import com.huawei.struts.dao.LjwAclentryTDAO;
import com.huawei.struts.dao.LjwPremissionTDAO;
import com.huawei.struts.exception.SystemException;
import com.huawei.struts.service.ICommonService;
import com.st.model.HtmlSelectOption;
import com.st.model.LjwAclentryT;
import com.st.model.LjwAclentryTId;
import com.st.model.LjwPremissionT;
import com.st.model.LjwUserT;

/**
 * @author Administrator
 *
 */
public class CommonService implements ICommonService {
	private LjwPremissionTDAO ljwPremissionTDAO;
	private LjwAclentryTDAO ljwAclentryTDAO;
	private CommonDAO commonDAO;
	
	public void setCommonDAO(CommonDAO commonDAO) {
		this.commonDAO = commonDAO;
	}


	public void setLjwAclentryTDAO(LjwAclentryTDAO ljwAclentryTDAO) {
		this.ljwAclentryTDAO = ljwAclentryTDAO;
	}


	public void setLjwPremissionTDAO(LjwPremissionTDAO ljwPremissionTDAO) {
		this.ljwPremissionTDAO = ljwPremissionTDAO;
	}


	/* (non-Javadoc)
	 * @see com.huawei.struts.service.ICommonService#synchronPermission(com.st.model.LjwPremissionT)
	 */
	public void synchronPermission(List<LjwPremissionT> ljwPremissionT)
			throws SystemException {
		try{
			for(int i=0;i<ljwPremissionT.size();i++){
				LjwPremissionT ljwPremissiont=ljwPremissionT.get(i);
				//判断是否存在
				if(ljwPremissionTDAO.checkExsits(ljwPremissiont)==0){
					ljwPremissionTDAO.save(ljwPremissiont);
				}
			}
		}catch (Exception e) {
			throw new SystemException("error.info", e.getCause());
		}

	}


	public List<LjwPremissionT> findAllPremission(int roleId) throws SystemException {
		return ljwPremissionTDAO.findPremission(roleId);
		
	}


	/**
	 * 保存信息
	 */
	public void saveRoleGrant(String[] permissionIds,Integer roleId) throws SystemException {
		if(permissionIds!=null){
			for(int i=0;i<permissionIds.length;i++){
				Integer permissionId=Integer.valueOf(permissionIds[i].toString());
				LjwAclentryTId ljwAclentryT=new LjwAclentryTId();
				ljwAclentryT.setRoleId(roleId);
				ljwAclentryT.setPermissionId(permissionId);
				LjwAclentryT aclentry=new LjwAclentryT();
				aclentry.setId(ljwAclentryT);
				ljwAclentryTDAO.save(aclentry);
			}
		}
		
	}
	
	/**
	 * 删除信息
	 */
	public void deleteAclentry(Integer roleId) throws SystemException{
		ljwAclentryTDAO.deleteAclentry(Integer.parseInt(roleId.toString()));
		 
	}


	/**
	 * 得到维度下拉框
	 */
	public List<HtmlSelectOption> getProgram() throws SystemException {
		// TODO Auto-generated method stub
		return commonDAO.getProgarmList();
	}


	/**
	 * 得到角色下拉框
	 */
	public List<HtmlSelectOption> getRoleList() throws SystemException {
		// TODO Auto-generated method stub
		return commonDAO.getRoleList();
	}


	public void saveUserRoleProgram(LjwUserT user) throws SystemException {
		try{
			LjwUserT ljwUser=commonDAO.getUserInfoByUserId(user.getUserId());
			if(ljwUser.getRoleId()!=null && ljwUser.getProgramId()!=null ){
				if(user.getRoleId()==0 || user.getProgramId()==0){
					commonDAO.deleteUserRoleProgram(user);
				}
				else{
					commonDAO.updateUserRoleProgram(user);
				}
				
			}
			else{
				commonDAO.saveUserRoleProgram(user);
			}
		} catch (Exception e) {
			throw new SystemException("error.info", e.getCause());
		}
	}


	public LjwUserT getUserInfoById(Integer userId) throws SystemException {
		// TODO Auto-generated method stub
		return commonDAO.getUserInfoByUserId(userId);
	}
	
	
	

}
